﻿#region Copyright © 2008, DevLynx (MIT License)
// This software is licensed under The MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion

namespace Test.DxExtensions
{
    using System;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using DevExpressContrib.Core.DxExtensions;
    using NUnit.Framework;
    using NUnit.Framework.SyntaxHelpers;

    [TestFixture]
    public class TestDxExtensions
    {
        #region Tests (5)

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void TestMemoBrokenTextBox()
        {
            using (MemoEdit memo = new MemoEdit())
            {
                if (memo != null)
                    for (int i = 0; i < memo.Controls.Count; i++)
                    {
                        if (memo.Controls[i] is TextBox)
                        {
                            memo.Controls.Remove(memo.Controls[i]);
                            break;
                        }
                    }
                // we have removed the underlying TextBox. How this could happen in 
                // a real world application, I don't know. But it's gone and should
                // throw an InvalidOperationException.
                memo.AppendLine("I'm broken and I can't get up...");
            }
        }

        [Test]
        public void TestMemoEditAppend()
        {
            using (MemoEdit memo = new MemoEdit())
            {
                Assert.That(memo.Lines.Length, Is.EqualTo(0));
                memo.Append("Testing...");
                Assert.That(memo.Lines.Length, Is.EqualTo(1));
                Assert.That(memo.Lines[0], Is.EqualTo("Testing..."));
                memo.Append("one, two, three...");
                Assert.That(memo.Lines.Length, Is.EqualTo(1));
                Assert.That(memo.Lines[0], Is.EqualTo("Testing...one, two, three..."));
            }
        }

        [Test]
        public void TestMemoEditAppendLine()
        {
            using (MemoEdit memo = new MemoEdit())
            {
                Assert.That(memo.Lines.Length, Is.EqualTo(0));
                memo.AppendLine("Test");
                Assert.That(memo.Lines.Length, Is.EqualTo(1));
                Assert.That(memo.Lines[0], Is.EqualTo("Test"));
                memo.AppendLine("Test 2");
                Assert.That(memo.Lines.Length, Is.EqualTo(2));
                Assert.That(memo.Lines[1], Is.EqualTo("Test 2"));
                memo.AppendLine();
                Assert.That(memo.Lines.Length, Is.EqualTo(3));
            }
        }

        [Test]
        public void TestMemoEditAppendLineNoScroll()
        {
            using (MemoEdit memo = new MemoEdit())
            {
                Assert.That(memo.Lines.Length, Is.EqualTo(0));
                memo.AppendLineNoScroll("Test");
                Assert.That(memo.Lines.Length, Is.EqualTo(1));
                Assert.That(memo.Lines[0], Is.EqualTo("Test"));
                memo.AppendLineNoScroll("Test 2");
                Assert.That(memo.Lines.Length, Is.EqualTo(2));
                Assert.That(memo.Lines[1], Is.EqualTo("Test 2"));
                memo.AppendLineNoScroll();
                Assert.That(memo.Lines.Length, Is.EqualTo(3));
            }
        }

        [Test]
        public void TestMemoEditNullText()
        {
            using (MemoEdit memo = new MemoEdit())
            {
                memo.Append(null);
                Assert.That(memo.Lines.Length, Is.EqualTo(0));
                memo.AppendLine(null);
                Assert.That(memo.Lines.Length, Is.EqualTo(0));
                memo.AppendLineNoScroll(null);
                Assert.That(memo.Lines.Length, Is.EqualTo(0));

                // now test with at least one line in the system.
                memo.Append("New Line");
                Assert.That(memo.Lines.Length, Is.EqualTo(1));
                memo.Append(null);
                Assert.That(memo.Lines.Length, Is.EqualTo(1));
                memo.AppendLine(null);
                Assert.That(memo.Lines.Length, Is.EqualTo(2));
                memo.AppendLineNoScroll(null);
                Assert.That(memo.Lines.Length, Is.EqualTo(3));
            }
        }

        #endregion Tests
    }
}
